<?php
/**
 * Class descript.
 * User: 广州利大科技 chan
 * Date: 2017/8/7 20:02
 */

namespace app\api\service;


use app\api\BaseModel;
use think\Model;

class Pay extends BaseModel
{

    private $pay_sn;


    public function __construct()
    {
        parent::__construct();
        $this->pay_sn = input('pay_sn/s');
        if(empty($this->pay_sn)) return;
    }

    /**
     * 返回支付数据
     * @param null|string $pay_id
     */
    private function getPayData()
    {
        $pay_info = db('pay_log')->where(array('pay_sn' => $this->pay_sn))->find();
        if(!$pay_info) return array('message' => '该订单不存在');
        if($pay_info['status'] == PAY_SUCCESS) return array('message' => '该订单已支付');
        $order = db('order')->where(['order_id' => $pay_info['record_id']])->find();
        if($order['order_state'] != ORDER_STATUS_NOPAY) return array('message' => '该订单状态不允许此操作');
        $data = array(
            'pay_sn'  => $pay_info['pay_sn'],
            'charges' => $pay_info['money'],
            'title'   => $order['product_name']
        );
        return $data;
    }

    public function WxAppPay()
    {
        $data = $this->getPayData();
        if(isset($data['message'])) return $data;
        return (new WxPay())->WxAppPay($data);
    }
    public function AliPay()
    {
        $data =  $this->getPayData();
        if(isset($data['message'])) return $data;
        return (new AliPay())->pay($data);
    }

}